<cfcomponent output="false">

	<cffunction name="read" output="false" access="public" returntype="slr.components.cfgenerated.Medicao_Detail.MedicaoBean">
		<cfargument name="id" required="true">
		<cfset var qRead="">
		<cfset var obj="">

		<cfquery name="qRead" datasource="BRUST">
			select 	equipamentoId, medicaoId, identificacaoSala, simboloPorta, avisoAcessoRestrito, lampadaIndicadora, 
					quantidadeTubos, acessorioProtecao, contratoDosimetria, cargaTrabalhoResultado, dataCadastro, 
					tecnicoCadastro, distanciaFocoFilme, cargaTrabalhoExames, cargaTrabalhoDias, cargaTrabalhoIncidencias, 
					cargaTrabalhoTecnica
			from dbo.Medicao
			where medicaoId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.id#" />
		</cfquery>

		<cfscript>
			obj = createObject("component", "slr.components.cfgenerated.Medicao_Detail.MedicaoBean").init();
			obj.setequipamentoId(qRead.equipamentoId);
			obj.setmedicaoId(qRead.medicaoId);
			obj.setidentificacaoSala(qRead.identificacaoSala);
			obj.setsimboloPorta(qRead.simboloPorta);
			obj.setavisoAcessoRestrito(qRead.avisoAcessoRestrito);
			obj.setlampadaIndicadora(qRead.lampadaIndicadora);
			obj.setquantidadeTubos(qRead.quantidadeTubos);
			obj.setacessorioProtecao(qRead.acessorioProtecao);
			obj.setcontratoDosimetria(qRead.contratoDosimetria);
			obj.setcargaTrabalhoResultado(qRead.cargaTrabalhoResultado);
			obj.setdataCadastro(qRead.dataCadastro);
			obj.settecnicoCadastro(qRead.tecnicoCadastro);
			obj.setdistanciaFocoFilme(qRead.distanciaFocoFilme);
			obj.setcargaTrabalhoExames(qRead.cargaTrabalhoExames);
			obj.setcargaTrabalhoDias(qRead.cargaTrabalhoDias);
			obj.setcargaTrabalhoIncidencias(qRead.cargaTrabalhoIncidencias);
			obj.setcargaTrabalhoTecnica(qRead.cargaTrabalhoTecnica);
			obj.setPontoInteresseBeans(createObject("component", "PontoInteresseGateway").getAll(medicaoId=qRead.medicaoId));
			obj.setTecnicaBeans(createObject("component", "TecnicaGateway").getAll(medicaoId=qRead.medicaoId));
			return obj;
		</cfscript>
	</cffunction>



	<cffunction name="create" output="false" access="public" returntype="void">
		<cfargument name="bean" required="true" type="slr.components.cfgenerated.Medicao_Detail.MedicaoBean">
		<cfset var qCreate="">

		<cfset var qGetId="">

		<cfset var local0=arguments.bean.getequipamentoId()>
		<cfset var local2=arguments.bean.getidentificacaoSala()>
		<cfset var local3=arguments.bean.getsimboloPorta()>
		<cfset var local4=arguments.bean.getavisoAcessoRestrito()>
		<cfset var local5=arguments.bean.getlampadaIndicadora()>
		<cfset var local6=arguments.bean.getquantidadeTubos()>
		<cfset var local7=arguments.bean.getacessorioProtecao()>
		<cfset var local8=arguments.bean.getcontratoDosimetria()>
		<cfset var local9=arguments.bean.getcargaTrabalhoResultado()>
		<cfset var local10=arguments.bean.getdataCadastro()>
		<cfset var local11=arguments.bean.gettecnicoCadastro()>
		<cfset var local12=arguments.bean.getdistanciaFocoFilme()>
		<cfset var local13=arguments.bean.getcargaTrabalhoExames()>
		<cfset var local14=arguments.bean.getcargaTrabalhoDias()>
		<cfset var local15=arguments.bean.getcargaTrabalhoIncidencias()>
		<cfset var local16=arguments.bean.getcargaTrabalhoTecnica()>

		<cftransaction isolation="read_committed">
			<cfquery name="qCreate" datasource="BRUST">
				insert into dbo.Medicao(equipamentoId, identificacaoSala, simboloPorta, avisoAcessoRestrito, lampadaIndicadora, quantidadeTubos, acessorioProtecao, contratoDosimetria, cargaTrabalhoResultado, dataCadastro, tecnicoCadastro, distanciaFocoFilme, cargaTrabalhoExames, cargaTrabalhoDias, cargaTrabalhoIncidencias, cargaTrabalhoTecnica)
				values (
					<cfqueryparam value="#local0#" cfsqltype="CF_SQL_INTEGER" null="#iif((local0 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local2#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local3#" cfsqltype="CF_SQL_BIT" null="#iif((local3 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local4#" cfsqltype="CF_SQL_BIT" null="#iif((local4 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local5#" cfsqltype="CF_SQL_BIT" null="#iif((local5 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local6#" cfsqltype="CF_SQL_INTEGER" null="#iif((local6 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local7#" cfsqltype="CF_SQL_BIT" null="#iif((local7 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local8#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local9#" cfsqltype="CF_SQL_FLOAT" null="#iif((local9 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local10#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((local10 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local11#" cfsqltype="CF_SQL_VARCHAR" />,
					<cfqueryparam value="#local12#" cfsqltype="CF_SQL_INTEGER" null="#iif((local12 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local13#" cfsqltype="CF_SQL_INTEGER" null="#iif((local13 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local14#" cfsqltype="CF_SQL_INTEGER" null="#iif((local14 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local15#" cfsqltype="CF_SQL_INTEGER" null="#iif((local15 eq ""), de("yes"), de("no"))#" />,
					<cfqueryparam value="#local16#" cfsqltype="CF_SQL_INTEGER" null="#iif((local16 eq ""), de("yes"), de("no"))#" />
				)
			</cfquery>

			<!--- If your server has a better way to get the ID that is more reliable, use that instead --->
			<cfquery name="qGetID" datasource="BRUST">
				select medicaoId
				from dbo.Medicao
				where equipamentoId = <cfqueryparam value="#local0#" cfsqltype="CF_SQL_INTEGER" null="#iif((local0 eq ""), de("yes"), de("no"))#" />
				  and identificacaoSala = <cfqueryparam value="#local2#" cfsqltype="CF_SQL_VARCHAR" />
				  and simboloPorta = <cfqueryparam value="#local3#" cfsqltype="CF_SQL_BIT" null="#iif((local3 eq ""), de("yes"), de("no"))#" />
				  and avisoAcessoRestrito = <cfqueryparam value="#local4#" cfsqltype="CF_SQL_BIT" null="#iif((local4 eq ""), de("yes"), de("no"))#" />
				  and lampadaIndicadora = <cfqueryparam value="#local5#" cfsqltype="CF_SQL_BIT" null="#iif((local5 eq ""), de("yes"), de("no"))#" />
				  and quantidadeTubos = <cfqueryparam value="#local6#" cfsqltype="CF_SQL_INTEGER" null="#iif((local6 eq ""), de("yes"), de("no"))#" />
				  and acessorioProtecao = <cfqueryparam value="#local7#" cfsqltype="CF_SQL_BIT" null="#iif((local7 eq ""), de("yes"), de("no"))#" />
				  and contratoDosimetria = <cfqueryparam value="#local8#" cfsqltype="CF_SQL_VARCHAR" />
				  and cargaTrabalhoResultado = <cfqueryparam value="#local9#" cfsqltype="CF_SQL_FLOAT" null="#iif((local9 eq ""), de("yes"), de("no"))#" />
				  and dataCadastro = <cfqueryparam value="#local10#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((local10 eq ""), de("yes"), de("no"))#" />
				  and tecnicoCadastro = <cfqueryparam value="#local11#" cfsqltype="CF_SQL_VARCHAR" />
				  and distanciaFocoFilme = <cfqueryparam value="#local12#" cfsqltype="CF_SQL_INTEGER" null="#iif((local12 eq ""), de("yes"), de("no"))#" />
				  and cargaTrabalhoExames = <cfqueryparam value="#local13#" cfsqltype="CF_SQL_INTEGER" null="#iif((local13 eq ""), de("yes"), de("no"))#" />
				  and cargaTrabalhoDias = <cfqueryparam value="#local14#" cfsqltype="CF_SQL_INTEGER" null="#iif((local14 eq ""), de("yes"), de("no"))#" />
				  and cargaTrabalhoIncidencias = <cfqueryparam value="#local15#" cfsqltype="CF_SQL_INTEGER" null="#iif((local15 eq ""), de("yes"), de("no"))#" />
				  and cargaTrabalhoTecnica = <cfqueryparam value="#local16#" cfsqltype="CF_SQL_INTEGER" null="#iif((local16 eq ""), de("yes"), de("no"))#" />
				order by medicaoId desc
			</cfquery>
		</cftransaction>

		<cfscript>
			arguments.bean.setmedicaoId(qGetID.medicaoId);
		</cfscript>

		<cfscript>
			PontoInteresseBeans = arguments.bean.getPontoInteresseBeans();
			PontoInteresseDao = createObject("component", "PontoInteresseDAO");

			for (i = 1; i LTE ArrayLen(PontoInteresseBeans); i = i + 1)
			{
				PontoInteresseBeans[i].setmedicaoId(arguments.bean.getmedicaoId());
				PontoInteresseDao.create(PontoInteresseBeans[i]);
			}
		</cfscript>
		
		<cfscript>
			TecnicaBeans = arguments.bean.getTecnicaBeans();
			TecnicaDao = createObject("component", "TecnicaDAO");

			for (i = 1; i LTE ArrayLen(TecnicaBeans); i = i + 1)
			{
				TecnicaBeans[i].setmedicaoId(arguments.bean.getmedicaoId());
				TecnicaDao.create(TecnicaBeans[i]);
			}
		</cfscript>
	</cffunction>



	<cffunction name="update" output="false" access="public" returntype="void">
		<cfargument name="bean" required="true" type="slr.components.cfgenerated.Medicao_Detail.MedicaoBean">
		<cfset var qUpdate="">

		<cftransaction isolation="read_committed">
			<cfquery name="qUpdate" datasource="BRUST" result="status">
				update dbo.Medicao
				set equipamentoId = <cfqueryparam value="#arguments.bean.getequipamentoId()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getequipamentoId() eq ""), de("yes"), de("no"))#" />,
					identificacaoSala = <cfqueryparam value="#arguments.bean.getidentificacaoSala()#" cfsqltype="CF_SQL_VARCHAR" />,
					simboloPorta = <cfqueryparam value="#arguments.bean.getsimboloPorta()#" cfsqltype="CF_SQL_BIT" null="#iif((arguments.bean.getsimboloPorta() eq ""), de("yes"), de("no"))#" />,
					avisoAcessoRestrito = <cfqueryparam value="#arguments.bean.getavisoAcessoRestrito()#" cfsqltype="CF_SQL_BIT" null="#iif((arguments.bean.getavisoAcessoRestrito() eq ""), de("yes"), de("no"))#" />,
					lampadaIndicadora = <cfqueryparam value="#arguments.bean.getlampadaIndicadora()#" cfsqltype="CF_SQL_BIT" null="#iif((arguments.bean.getlampadaIndicadora() eq ""), de("yes"), de("no"))#" />,
					quantidadeTubos = <cfqueryparam value="#arguments.bean.getquantidadeTubos()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getquantidadeTubos() eq ""), de("yes"), de("no"))#" />,
					acessorioProtecao = <cfqueryparam value="#arguments.bean.getacessorioProtecao()#" cfsqltype="CF_SQL_BIT" null="#iif((arguments.bean.getacessorioProtecao() eq ""), de("yes"), de("no"))#" />,
					contratoDosimetria = <cfqueryparam value="#arguments.bean.getcontratoDosimetria()#" cfsqltype="CF_SQL_VARCHAR" />,
					cargaTrabalhoResultado = <cfqueryparam value="#arguments.bean.getcargaTrabalhoResultado()#" cfsqltype="CF_SQL_FLOAT" null="#iif((arguments.bean.getcargaTrabalhoResultado() eq ""), de("yes"), de("no"))#" />,
					dataCadastro = <cfqueryparam value="#arguments.bean.getdataCadastro()#" cfsqltype="CF_SQL_TIMESTAMP" null="#iif((arguments.bean.getdataCadastro() eq ""), de("yes"), de("no"))#" />,
					tecnicoCadastro = <cfqueryparam value="#arguments.bean.gettecnicoCadastro()#" cfsqltype="CF_SQL_VARCHAR" />,
					distanciaFocoFilme = <cfqueryparam value="#arguments.bean.getdistanciaFocoFilme()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getdistanciaFocoFilme() eq ""), de("yes"), de("no"))#" />,
					cargaTrabalhoExames = <cfqueryparam value="#arguments.bean.getcargaTrabalhoExames()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getcargaTrabalhoExames() eq ""), de("yes"), de("no"))#" />,
					cargaTrabalhoDias = <cfqueryparam value="#arguments.bean.getcargaTrabalhoDias()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getcargaTrabalhoDias() eq ""), de("yes"), de("no"))#" />,
					cargaTrabalhoIncidencias = <cfqueryparam value="#arguments.bean.getcargaTrabalhoIncidencias()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getcargaTrabalhoIncidencias() eq ""), de("yes"), de("no"))#" />,
					cargaTrabalhoTecnica = <cfqueryparam value="#arguments.bean.getcargaTrabalhoTecnica()#" cfsqltype="CF_SQL_INTEGER" null="#iif((arguments.bean.getcargaTrabalhoTecnica() eq ""), de("yes"), de("no"))#" />
				where medicaoId = <cfqueryparam value="#arguments.bean.getmedicaoId()#" cfsqltype="CF_SQL_INTEGER">
			</cfquery>

			<cfscript>
				// Update all Child Component properties.
				PontoInteresseBeans = arguments.bean.getPontoInteresseBeans();
				PontoInteresseDao = createObject("component", "PontoInteresseDAO");
				PontoInteresseGateway = createObject("component", "PontoInteresseGateway");

				// return all of the primary keys (linked to this object) in the database.
				oldpontoInteresseIds = PontoInteresseGateway.getAllAsQuery(fieldlist="pontoInteresseId", medicaoId=arguments.bean.getmedicaoId());

				beansToDelete = ArrayNew(1);

				// compare the current item ids with the ids pulled out of the database.
				// to find the items that should be deleted.
				for (j = 1; j LTE oldpontoInteresseIds.recordCount; j = j + 1)
				{
					found = false;
					for (i = ArrayLen(PontoInteresseBeans); i GTE 1; i = i - 1)
					{
					lookupBean = PontoInteresseBeans[i];
						if (oldpontoInteresseIds.pontoInteresseId[j] EQ lookupBean.getpontoInteresseId())
						{
							found = true;
							break;
						}
					}
					
					// if we find it in the database, but not in the array, then it was deleted
					if (found EQ false)
					{
						ArrayAppend(beansToDelete, PontoInteresseDao.read(oldpontoInteresseIds.pontoInteresseId[j]));
					}
				}
				
				// delete the ones that are in the to be deleted array
				for (i = 1; i LTE ArrayLen(beansToDelete); i = i + 1)
				{
					PontoInteresseDao.delete(beansToDelete[i]);
				}
				
				// update or save the ones that are left
				for (i = 1; i LTE ArrayLen(PontoInteresseBeans); i = i + 1)
				{
					PontoInteresseBeans[i].setmedicaoId(arguments.bean.getmedicaoId());
					PontoInteresseGateway.save(PontoInteresseBeans[i]);
				}
			</cfscript>
			
			<cfscript>
				// Update all Child Component properties.
				TecnicaBeans = arguments.bean.getTecnicaBeans();
				TecnicaDao = createObject("component", "TecnicaDAO");
				TecnicaGateway = createObject("component", "TecnicaGateway");

				// return all of the primary keys (linked to this object) in the database.
				oldTecnicaIds = TecnicaGateway.getAllAsQuery(fieldlist="TecnicaId", medicaoId=arguments.bean.getmedicaoId());

				beansToDelete = ArrayNew(1);

				// compare the current item ids with the ids pulled out of the database.
				// to find the items that should be deleted.
				for (j = 1; j LTE oldTecnicaIds.recordCount; j = j + 1)
				{
					found = false;
					for (i = ArrayLen(TecnicaBeans); i GTE 1; i = i - 1)
					{
					lookupBean = TecnicaBeans[i];
						if (oldTecnicaIds.tecnicaId[j] EQ lookupBean.getTecnicaId())
						{
							found = true;
							break;
						}
					}
					
					// if we find it in the database, but not in the array, then it was deleted
					if (found EQ false)
					{
						ArrayAppend(beansToDelete, TecnicaDao.read(oldTecnicaIds.TecnicaId[j]));
					}
				}
				
				// delete the ones that are in the to be deleted array
				for (i = 1; i LTE ArrayLen(beansToDelete); i = i + 1)
				{
					TecnicaDao.delete(beansToDelete[i]);
				}
				
				// update or save the ones that are left
				for (i = 1; i LTE ArrayLen(TecnicaBeans); i = i + 1)
				{
					TecnicaBeans[i].setmedicaoId(arguments.bean.getmedicaoId());
					TecnicaGateway.save(TecnicaBeans[i]);
				}
			</cfscript>
		</cftransaction>
	</cffunction>



	<cffunction name="delete" output="false" access="public" returntype="void">
		<cfargument name="bean" required="true" type="slr.components.cfgenerated.Medicao_Detail.MedicaoBean">
		<cfset var qDelete="">

		<cftransaction isolation="read_committed">
			<cfscript>
				PontoInteresseBeans = arguments.bean.getPontoInteresseBeans();
				PontoInteresseDao = createObject("component", "PontoInteresseDAO");

				for (i = 1; i LTE ArrayLen(PontoInteresseBeans); i = i + 1)
				{
					PontoInteresseDao.delete(PontoInteresseBeans[i]);
				}
			</cfscript>

			<cfquery name="qDelete" datasource="BRUST" result="status">
				delete
				from dbo.Medicao
				where medicaoId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.bean.getmedicaoId()#" />
			</cfquery>
		</cftransaction>

	</cffunction>


</cfcomponent>